﻿using System;
using System.Data.SqlClient;
using AuctionSite.Entities;

namespace AuctionSite.DAL
{
    public class SubCategoriaDAL
    {
        public SubCategoria Incluir(SubCategoria subCategoria)
        {
            SqlConnection cn = new SqlConnection();
            try
            {
                
                cn.ConnectionString = Dados.StringDeConexao;
                //command
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;

                cmd.CommandText = "INSERT INTO subcategorias (sub_nome, cat_id) VALUES (@sub_nome,@cat_id);select @@IDENTITY;";

                cmd.Parameters.AddWithValue("@sub_nome", subCategoria.Nome);
                cmd.Parameters.AddWithValue("@cat_id", subCategoria.CategoriaId);

                cn.Open();

                subCategoria.Id = Convert.ToInt32(cmd.ExecuteScalar());

                return subCategoria;

            }
            catch (SqlException ex)
            {
                throw new Exception("Servidor SQL Erro:" + ex.Number);
            }
            catch (Exception ex)
            {
                throw new Exception("Erro:" + ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }
    }
}